<template>
  <!-- tab基本信息表单  -->
  <el-scrollbar style="height: 100%;margin-top:10px;">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="90px"
      class="form-input-m"
      inline-message
    >

      <el-form-item label="编号" prop="layerCode">
        <el-input v-model="formData.layerCode" placeholder="请输入编号" clearable></el-input>
      </el-form-item>
      <el-form-item label="条码号" prop="layerBarcode">
        <el-input v-model="formData.layerBarcode" placeholder="请输入条码号" clearable></el-input>
      </el-form-item>
      <el-form-item label="支持温度" prop="supportTemp">
        <el-input v-model="formData.supportTemp" placeholder="请输入支持温度" clearable></el-input>
      </el-form-item>
      <el-form-item label="当前温度" prop="currentTemp">
        <el-input v-model="formData.currentTemp" placeholder="请输入当前温度" clearable></el-input>
      </el-form-item>
      <el-form-item label="尺寸" prop="layerSize">
        <el-input v-model="formData.layerSize" placeholder="请输入尺寸" clearable></el-input>
      </el-form-item>
<!--      <el-form-item label="支持标本盒类型" prop="boxTypesRange">-->
<!--        <el-input v-model="formData.boxTypesRange" placeholder="请输入支持标本盒类型" clearable></el-input>-->
<!--      </el-form-item>-->
      <el-form-item label="位置排序" prop="layerSeq">
        <el-input v-model="formData.layerSeq" placeholder="请输入位置排序" clearable></el-input>
      </el-form-item>
<!--      <el-form-item label="所属科室" prop="deptName">-->
<!--        <SelectDataGrid :url="'/api/sys/getDepartmentList'" :column="[-->
<!--                  { field: 'code', title: '科室编码' }, //列标题-->
<!--                  { field: 'name', title: '科室名称' }-->
<!--                ]" :searchParam="{ orgId }" :isPage="false" @SelectCellClick="-->
<!--                  (e) => {-->
<!--                    selectRow(e, rowIndex);-->
<!--                  }-->
<!--                " :width="'200px'">-->
<!--        </SelectDataGrid>-->
<!--      </el-form-item>-->
      <el-form-item label="" prop="">
        <el-button type="primary" @click="saveForm">保存</el-button>
      </el-form-item>
    </el-form>
  </el-scrollbar>
</template>

<script lang="ts" setup>
// 引入组件区 ==========
import {
  ref,
  reactive,
  watch,
  getCurrentInstance,
  PropType,
  onMounted
} from 'vue';
import { IsEmpty, formatDateTime } from '@/utils/common'; //导入工具函数
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage';
import { funBdsJsResult } from '@/utils/Expression'; //函数公式
import SelectDataGrid from '@/components/SelectDataGrid.vue';
import { AppStore } from '@/stores/store';
import {  FormInstance } from 'element-plus';
import {
samstoFridgeLayerAdd,
samstoFridgeLayerEdit
} from '@/views/lab_sys/samsto_set/apis/samsto_api';

// 全局变量定义区 ==========
let { proxy }: any = getCurrentInstance();
let store = AppStore();
let orgId = ref(null);
const formRef = ref<FormInstance>; //表单ref标记
// 表单绑定的数据类型限制
interface FormDataVO {
            id: string /*主键*/;
  	  			layerCode: String ;/*编号 */
	    	  			layerBarcode: String ;/*条码号 */
	    	  			supportTemp: String ;/*支持温度 */
	    	  			currentTemp: String ;/*当前温度 */
	    	  			boxTypesRange: String ;/*支持标本盒类型 */
	    	  			layerType: String ;/*类型 */
	    	  			layerSize: String ;/*尺寸 */
	    	  			fridgeId: String ;/*冰箱 */
	    	  			layerSeq: String ;/*位置排序 */
	    	  			status: Number ;/*状态 */
		  	  			deptName: String ;/*所属科室 */
	    }
// 表单绑定的数据
let formData = reactive<FormDataVO>({
            id: '',
      			layerCode: '' ,/*编号 */
	      			layerBarcode: '' ,/*条码号 */
	      			supportTemp: '' ,/*支持温度 */
	      			currentTemp: '' ,/*当前温度 */
	      			boxTypesRange: '' ,/*支持标本盒类型 */
	      			layerType: '' ,/*类型 */
	      			layerSize: '' ,/*尺寸 */
	      			fridgeId: '' ,/*冰箱 */
	      			layerSeq: '' ,/*位置排序 */
	      			status: 1 ,/*状态 */
		      			deptName: '' ,/*所属科室 */
	  });
let types = reactive([]);
let fridgeId=ref("");
// 表单校验规则
const formRules: any = {
  layerCode:[{ required: true, message: '请输入编号' }],
  layerBarcode:[{ required: true, message: '请输入条码号' }]
};


defineExpose({
  addNew,
  saveForm,
  initPage
});

// 父组件传值
let props = defineProps({
  fridgeId: { type: String as PropType<any>, required: true },
  currentRow: { type: Object as PropType<any>, required: true }
}) as any;
watch(
  () => props.currentRow,
  (nval, oval) => {
    Object.assign(formData, nval);
  },
  {
    deep: true,
    immediate: true
  }
);
watch(
  () => props.fridgeId,
  (nval, oval) => {
    fridgeId.value=nval;
  },
  {
    deep: true,
    immediate: true
  }
);


onMounted(() => {
  orgId = store.userInfo.orgId;
  //formData.bankNumber=generateCode();
});


// 方法定义区 =============
//生成题库编码
function generateCode() {
  let now = new Date();
  console.log(now,"now");
  let y = now.getFullYear(),
    m = now.getMonth() + 1,
    d = now.getDate(),
    h = now.getHours(),
    mm = now.getMinutes(),
    s = now.getSeconds(),
    ms = now.getMilliseconds();
  return "TM"+y + m+d+h+mm+s+ms;
}
function addNew(callback: any) {
  clear();
  if (callback) {
    callback({ a: '初始化完成' });
  }
}
// 清空表单
function clear() {
  //formData.startus = 1;
  //formData.bankNumber = generateCode();
  //formData.seq = 1;
  formData.id = "";
    			formData.layerCode= '' ;/*编号 */
	    			formData.layerBarcode= '' ;/*条码号 */
	    			formData.supportTemp= '' ;/*支持温度 */
	    			formData.currentTemp= '' ;/*当前温度 */
	    			formData.boxTypesRange= '' ;/*支持标本盒类型 */
	    			formData.layerType= '' ;/*类型 */
	    			formData.layerSize= '' ;/*尺寸 */
	    			formData.fridgeId= fridgeId.value ;/*冰箱 */
	    			formData.layerSeq= '' ;/*位置排序 */
	    			formData.status= 1 ;/*状态 */
		    			formData.deptName= '' ;/*所属科室 */
}

// 保存信息
function saveForm() {
  submitForm();
}
function initPage (selsectRow:any){
  Object.assign(formData, selsectRow);
  fridgeId.value=selsectRow.id;
}
// 保存
const submitForm = () => {
  let elInfo = proxy.$refs.formRef;
  formData.fridgeId=fridgeId.value;
  elInfo.validate(async (valid: any) => {
    if (!valid) {
      return false;
    }
    let res: any;
    if (IsEmpty(formData.id)) {
      res = await samstoFridgeLayerAdd(formData);
    } else {
      res = await samstoFridgeLayerEdit(formData);
    }
    res.status.code === 1 ? VxeMessage(res.status.msg, VxeNotifyType.success) : VxeMessage(res.status.msg, VxeNotifyType.error);
    clear();
    proxy.$emit('findList');
  });
};
// function selectRow(e, rowIndex) {
//   if (!IsEmpty(e)) {
//     formData.deptName = e.code;
//   }
// };
// const states = [
//   {
//     value: 1,
//     label: '启用'
//   },
//   {
//     value: 0,
//     label: '停用'
//   }
// ];
</script>

<style lang="scss" scoped>
// 中等尺寸时 配置扩展属性按钮
.el-button--default {
  padding: 5px 11px;
}
</style>

